package com.gmail.dmitryustimov.ms.java7.chain_of_responsibility.mod.mod_multy_type_actionmap.testperf;

import com.gmail.dmitryustimov.ms.java7.chain_of_responsibility.mod.mod_multy_type_actionmap.demo.handlers6_requests5.*;
import com.gmail.dmitryustimov.ms.java7.patternslib.chain_of_responsibility.multyevent.Handler;

public class TestPerfHandlers6Request5All {

    private static final int NUM_HANDLES = 1000000;
    private static final int NUM_WARM_UP = 3;
    private static final int NUM_TESTS = 100;

    private static final String MESSAGE = "chain_of_responsibility_mod - actionmap, 6 handlers, 5 request types, handleChain 4-5-6, took: ";

    public static void main(String[] args) {

        //warm up
        for (int i = 0; i < NUM_WARM_UP; i++) {
            test();
        }

        //tests
        long time = 0;
        for (int j = 0; j < NUM_TESTS; j++) {
            time += test();
        }
        System.out.println("final - " + MESSAGE + time/NUM_TESTS + "ns (" + (time/NUM_TESTS)/1000000 + "ms)");
    }

    private static long test() {
        Handler<Request> h1 = new CocreteHandler1<>("CocreteHandler1", 1);
        Handler<Request> h2 = new CocreteHandler2<>("CocreteHandler2", 2);
        Handler<Request> h3 = new CocreteHandler3<>("CocreteHandler3", 3);
        Handler<Request> h4 = new CocreteHandler4<>("CocreteHandler4", 4);
        Handler<Request> h5 = new CocreteHandler5<>("CocreteHandler5", 5);
        Handler<Request> h6 = new CocreteHandler6<>("CocreteHandler6", 6);

        h1.setNext(h2);
        h2.setNext(h3);
        h3.setNext(h4);
        h4.setNext(h5);
        h5.setNext(h6);

        Request request1 = new Request1("Request1", 4);
        Request request2 = new Request2("Request2", 4);
        Request request3 = new Request3("Request3", 4);
        Request request4 = new Request4("Request4", 4);
        Request request5 = new Request5("Request5", 4);

        long startTime = System.nanoTime();

        for (long j = 0; j < NUM_HANDLES; j++) {
            h1.handleChain(request1);
            h1.handleChain(request2);
            h1.handleChain(request3);
            h1.handleChain(request4);
            h1.handleChain(request5);
        }

        long estimatedTime = System.nanoTime() - startTime;
        System.out.println(MESSAGE + estimatedTime + "ns");
        return estimatedTime;
    }
}
